#include "FileTest.h"
#include <stdio.h>
#include <string.h>

/*
 * Created: 2/24/12; CGP
 
a) Create a new file (opened for writing)
b) With a SINGLE write call, write enough data into the file so that the data spans at least 3 blocks.
c) Close the file
d) Open the same file for reading. 
e) Read all the data & verify that the data was the same as expected. 
    Use a SINGLE read call, requesting the same amount of data read in your write call.
f) Make sure that when you read past the end of the file, you can't read more data. 
    (i.e., End of File is detected correctly).
 */

#define DATA_SIZE (508*3)

int main() {
    int length;
    printf("Starting test...\n"); fflush(stdout);
    FileTest ft;
    const char *fileName = "test1";

    File *f1 = new File(fileName, "w");
    unsigned char data[DATA_SIZE];
    for (int i=0; i < DATA_SIZE; i++) {
        data[i] = 'a' + (i % 30); // initialize with some ASCII chars
    }   

    length = f1->Write(data, DATA_SIZE);
    if (length != DATA_SIZE) {
        printf("ERROR: Could not write required amount of data (actual %d !=  expected %d)\n", 
            length, DATA_SIZE);
    }

    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;

    f1 = new File(fileName, "r");
    unsigned char buffer[DATA_SIZE];
    length = f1->Read(buffer, DATA_SIZE);
        
    if (length != DATA_SIZE) {
        printf("ERROR: Could not read back all %d bytes of file\n", DATA_SIZE);
    }
    
    if (memcmp((const char *) data, (const char *) buffer, DATA_SIZE) != 0) {
        printf("ERROR: Buffer is not equal to data written\n");
    }

    length = f1->Read(buffer, 1);
    if (length != 0) {
        printf("ERROR: End of file not correctly reached\n");
    }

    if (! f1->Close()) {
        printf("ERROR: Could not close file\n");
    }
    delete f1;
}




